<%@ page contentType="text/html;charset=UTF-8" %>
<%@ taglib uri="http://www.ufida.com/lfw" prefix="lfw" %>
<%@ taglib uri="http://www.ufida.com/uif" prefix="uif" %>
<%@ taglib uri="http://www.ufida.com/lfwtool" prefix="lfwtool" %> 
<html>
	<head>
		<lfw:base/>
		<lfw:head/>
		<uif:import/>
		<script type='text/javascript' src='<%= "code/" + request.getParameter("pageId") %>'></script>
	</head>
	<body bottommargin="0" topmargin="0" leftmargin="0" rightmargin="0" scroll=no onload="pageBodyScript()">
		<lfw:pageModel className="nc.lfw.core.model.FromPublicPageModel">
			<lfw:layout type="border">
				<lfw:layoutPanel position="center">
					<div id="listtt"></div>
				</lfw:layoutPanel>
				<lfw:layoutPanel position="bottom">
					<lfw:layout type="flowh">
						<lfw:layoutPanel>
							<lfw:button id="okbt"/>
						</lfw:layoutPanel>
						<lfw:layoutPanel>
							<lfw:button id="cancelbt"/>
						</lfw:layoutPanel>
					</lfw:layout>
				</lfw:layoutPanel>
			</lfw:layout>
		</lfw:pageModel>
		<script>
		function externalInit(){
			var listtt = new ListToListComp(document.getElementById("listtt"),"pflisttolist","0","0","650","450","absolute","全部","选定",true);
			listtt.setLeftTree(getLeftTree(listtt))
			window["$c_pflisttolist"] = listtt;
		}

		function getLeftTree(listToList){
			var leftTree = new TreeViewComp(listToList.leftPanel, "leftTree", 0,0,"100%","100%","relative",true,false,null,null);
			var lfetTreeLevel = new TreeLevel("lfetTreeLevel", "userDs","Recursive",["pk_user"],["ppk"],["username"],["pk_user"],"null",null,null);
			leftTree.setTreeLevel(lfetTreeLevel);
			leftTree.setDataset(initDefaultDsData());
			return leftTree;
		}
		/**
		 * 返回树节点和创建有列表项之间的关系
		 * [option,value]
		 */
		ListToListComp.prototype.getNodeOptionMap = function(node){
			/*
			 var ds = node.dataset; 
			 var row = node.nodeData;
			 var ppk = row.getCellValue(ds.nameToIndex('ppk'));
			 if(ppk == null || ppk == '')
				 return null;
			 var caption = row.getCellValue(ds.nameToIndex('username'));
			 var value = row.getCellValue(ds.nameToIndex('pk_user'));
			 */
			 return ["username",["pk_user"]];
		}
		
		ListToListComp.prototype.getTreeNodeFilter = function(treeNode){
			return treeNode.isLeaf;
		}
		
		function initDefaultDsData(){
			var obj =  parent.window.assignparams;
			if(obj == null)
				obj = parent.parent.window.assignparams;//當是在審批時打開指派框
			var assign_str = obj.toString();
			var dom = createXmlDom(assign_str);
			var rootNode = dom.documentElement;
			var roleList = rootNode.childNodes;
			var ds = getDataset("userDs");
			if( roleList != null && roleList.length > 0){
				for(var i = 0; i < roleList.length; i ++){
					var role = roleList[i];
					var rolecode = "role_id_"+i;
					addRole(ds, rolecode,role.getAttribute("name"));
					var userList = role.childNodes;
					if(userList != null && userList.length > 0){
						for(var k=0;k < userList.length; k++){
							var user = userList[k];
							addUser(ds, rolecode,user.childNodes);
						}
					}
				}
			}
			return ds;
		}
		
		function addRole(ds, rolecode,rolename){
			var row = ds.getEmptyRow();
			row.setCellValue(0,rolecode);
			row.setCellValue(2,rolename);
			ds.addRow(row,false); 
		}
		function addUser(ds, rolecode,nodes){
			var row = ds.getEmptyRow();
			row.setCellValue(0,nodes[0].text);
			row.setCellValue(1,nodes[1].text);
			row.setCellValue(2,nodes[2].text);
			row.setCellValue(3,nodes[3].text);
			row.setCellValue(4,nodes[4].text);
			row.setCellValue(5,rolecode);
			ds.addRow(row,false); 
		}
		</script>
	</body>
</html>